<!-- 本示例未包含完整css，获取外链css请参考上文，在hello uni-app项目中查看 -->
<template>
	<view>
		<view>
			<form @submit="formSubmit" @reset="formReset">
				<view class="uni-form-item uni-column">
					<view class="title">姓名</view>
					<input class="input" name="name" type="text" placeholder="请输入用户名" v-model="name" @blur="blurText" />
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">密码</view>
					<input class="input" name="password" type="password" placeholder="请输入密码" v-model="password"
						@blur="blurText1" />
				</view>
				<view class="uni-form-item uni-column">
					<view class="title">性别</view>
					<radio-group name="sex">
						<label>
							<radio value="男" /><text>男</text>
						</label>
						<label>
							<radio value="女" /><text>女</text>
						</label>
					</radio-group>
				</view>
				<view class="uni-btn-v">
					<button form-type="submit" data-eventsync="true">提交</button>
					<button type="default" form-type="reset" data-eventsync="true">重置</button>
				</view>
			</form>
		</view>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				name: "",
				password: "",
				sex: "",
			}
		},
		methods: {
			blurText(e) {
				if (e != null) {

				}
				console.log(e)
				console.log("blur事件被执行了")
			},
			blurText1() {
				console.log("blur事件被执行了")
			},
			formSubmit: function(e) {
				var that = this;
				var name = e.detail.value.name;
				var password = e.detail.value.password;
				var sex = e.detail.value.sex;

				uni.request({
					url: 'http://localhost:8090/user/register',
					method: 'POST',
					data: JSON.stringify({
						name: name,
						password: password,
						sex: sex,
					}),
					dataType: 'json',
					success: (res) => {
						if (res.data.data.code == "201") {
							uni.showToast({
								title: '注册失败，用户名已存在',
								icon: 'none',
								duration: 2000
							});
						} else {
							uni.showModal({
								title: '提示',
								content: '确定提交',
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定');
										uni.showToast({
											title: '注册成功',
											duration: 2000
										})
										uni.navigateTo({
											url: encodeURI('/pages/index/index')
										})
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							});

						}
					}
				})
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 10rpx 20rpx;
	}
</style>
